//
//  HomePageViewController.m
//  ShiWuPai
//
//  Created by app on 16/11/23.
//  Copyright © 2016年 QQ:244987427. All rights reserved.
//

#import "HomePageViewController.h"
#import "BaseNetworkRequest.h"
#import "DetailsViewController.h"
#import "HomePageDetailsViewController.h"

#import "BaseNetworkRequest+GuangChi.h"
#import "NSString+FJAddition.h"

#import "WHFeedInfor.h"
#import "WHCollectionViewCell.h"
#import <Masonry.h>
#import <UIKit+AFNetworking.h>
#import <SVPullToRefresh.h>
#define SELF_VIEW_WIDTH self.view.bounds.size.width
#define SELF_VIEW_HEIGHT self.view.bounds.size.height
@interface HomePageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation HomePageViewController

#pragma mark - life cycle
-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor grayColor];
    [self addSubviews];
    [self addConstraint];
    self.collectionView.backgroundColor=[UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"WHCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WHCollectionViewCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"WHCollectionViewCellImageCell" bundle:nil] forCellWithReuseIdentifier:@"WHCollectionViewCellImageCell"];
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    
    __weak typeof (HomePageViewController) *weakSelf=self;
    [self.collectionView addPullToRefreshWithActionHandler:^{
        weakSelf.page=1;
        [weakSelf requestData];
    }];
    
    [self.collectionView addInfiniteScrollingWithActionHandler:^{
        weakSelf.page++;
        [weakSelf requestData];
    }];
   
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.page=1;
    [self requestData];
}

#pragma mark - initViews
-(void)addSubviews{
    UICollectionViewFlowLayout *layout=[UICollectionViewFlowLayout new];
    layout.minimumLineSpacing=10;
    layout.minimumInteritemSpacing=5;
    layout.itemSize=CGSizeMake(SELF_VIEW_WIDTH/2-5, (SELF_VIEW_WIDTH/2-5)*1.25);
    /*
     设置布局
     */
    self.collectionView=[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    [self.view addSubview:self.collectionView];
}

-(void)addConstraint{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.view);
        make.leading.trailing.mas_equalTo(0);
    }];
}


#pragma mark - network request
-(void)requestData{
    
    [BaseNetworkRequest getFeedInfoWithPage:self.page CateGory:1 success:^(id responseObj, NSError *error) {
        if (!error) {
            WHFeedInfor *feedInfor=responseObj;
            if (self.page==1) {
                //下拉刷新
                [self.dataArray removeAllObjects];
                [self.dataArray addObjectsFromArray:feedInfor.feeds];
                [self.collectionView.pullToRefreshView stopAnimating];
                [self.collectionView reloadData];
            }
            
            else{
                //上拉加载
                [self.dataArray addObjectsFromArray:feedInfor.feeds];
                [self.collectionView.infiniteScrollingView stopAnimating];
                [self.collectionView reloadData];
            }
            
            
        }
        
        else{
            //错误提示
        }
    }];
}
#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WHFeed *feed=self.dataArray[indexPath.row];
    WHCollectionViewCell *cell;
    if (feed.contentType==6) {
        cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"WHCollectionViewCellImageCell" forIndexPath:indexPath];
        [cell.specialImageView setImageWithURL:[NSURL URLWithString:feed.cardImage] placeholderImage:nil];
    }
    else{
        
        cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"WHCollectionViewCell" forIndexPath:indexPath];
    }
     cell.feed=feed;
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
#pragma mark - UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WHFeed *feed=self.dataArray[indexPath.row];
    if (feed.contentType==6) {
        DetailsViewController *detailsViewController=[[DetailsViewController alloc] init];
        detailsViewController.linkURL=[NSURL URLWithString:feed.link];
        [self.navigationController pushViewController:detailsViewController animated:YES];
    }
    
    else{
        HomePageDetailsViewController * detailsViewController=[[HomePageDetailsViewController alloc] init];
        detailsViewController.feed=feed;
            [self.navigationController pushViewController:
             detailsViewController animated:YES];
    }
}

#pragma mark - setter getter
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

@end
